﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.IO;
using System.Collections.Generic;
public class test : MonoBehaviour {
	RenderTexture shadowTexture;
	// Use this for initialization
	Camera camera;
	Camera camera1;
	Shader depthShader;
	Projector proj;
	Matrix4x4 matVP;
	Matrix4x4 m_projMatrix;
	void Start () {
		depthShader = Shader.Find ("depthShader");
		camera = GameObject.Find ("Main Camera").camera;

		camera1 = GameObject.Find ("Camera").camera;
		//camera1.hideFlags = HideFlags.HideAndDontSave;
		camera1.enabled = false;
		//camera1.projectionMatrix = camera.projectionMatrix;
		int textureSize = 2048;
		shadowTexture = new RenderTexture(textureSize , textureSize, 0, RenderTextureFormat.ARGB32);
		shadowTexture.name = "shadowTexture" + GetInstanceID();
		shadowTexture.isPowerOfTwo = true;
		shadowTexture.hideFlags = HideFlags.DontSave;
		camera1.targetTexture = shadowTexture;
		proj = GameObject.Find ("GameObject").GetComponent<Projector> ();
		proj.nearClipPlane = camera.nearClipPlane;
		proj.farClipPlane = camera.farClipPlane;
		proj.fieldOfView = camera.fieldOfView;
		matVP = GL.GetGPUProjectionMatrix (camera1.projectionMatrix, true) * camera1.worldToCameraMatrix;
		proj.material.SetMatrix("ShadowMatrix", matVP);
		//camera1.SetReplacementShader (Shader.Find ("depthShader"), "");
	}

	// Update is called once per frame
	void Update () {
		CreateCameraProjecterMatrix ();
		proj.material.SetMatrix("ShadowMatrix", matVP);
		camera1.Render ();
		proj.material.SetTexture ("_ShadowTex", shadowTexture);
		//Debug.Log(camera1.worldToCameraMatrix.MultiplyPoint(GameObject.Find("wushi").transform.position));
		if(Input.GetKey(KeyCode.A))
		{
			Texture2D myTexture2D = new Texture2D(shadowTexture.width, shadowTexture.height);
			RenderTexture.active = shadowTexture;
			myTexture2D.ReadPixels(new Rect(0, 0, shadowTexture.width, shadowTexture.height), 0, 0);
			myTexture2D.Apply();
			RenderTexture.active = null;;
			File.WriteAllBytes (Application.streamingAssetsPath + "/11.png", myTexture2D.EncodeToPNG ());
		}

	}

	public List<Transform> CharactorList;
	void CreateCameraProjecterMatrix()
	{
		Vector3 v3MaxPosition = -Vector3.one * 500000.0f;
		Vector3 v3MinPosition = Vector3.one * 500000.0f;	
		for (int vertId = 0; vertId < CharactorList.Count; ++vertId)
		{
			// Light view space
			Vector3 v3Position = camera1.worldToCameraMatrix.MultiplyPoint3x4(CharactorList[vertId].position);
			if (v3Position.x > v3MaxPosition.x)
			{
				v3MaxPosition.x = v3Position.x;
			}
			if (v3Position.y > v3MaxPosition.y)
			{
				v3MaxPosition.y = v3Position.y;
			}
			if (v3Position.z > v3MaxPosition.z)
			{
				v3MaxPosition.z = v3Position.z;
			}
			if (v3Position.x < v3MinPosition.x)
			{
				v3MinPosition.x = v3Position.x;
			}
			if (v3Position.y < v3MinPosition.y)
			{
				v3MinPosition.y = v3Position.y;
			}
			if (v3Position.z < v3MinPosition.z)
			{
				v3MinPosition.z = v3Position.z;
			}
		}
		Vector3 off = v3MaxPosition - v3MinPosition;
		Vector3 sizeOff = off;
		sizeOff.z = 0;
		float dis = sizeOff.magnitude;
		//CreateOrthogonalProjectMatrix (ref m_projMatrix, v3MaxPosition, v3MinPosition);
		//Debug.Log (v3MaxPosition.ToString() + v3MinPosition.ToString());
		//Matrix4x4 m = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(1, 1, -1));
		//camera1.projectionMatrix = m * m_projMatrix;
		camera1.orthographicSize = dis / 1.8f;
		camera1.farClipPlane = off.z + 50;
		matVP = GL.GetGPUProjectionMatrix (camera1.projectionMatrix, true) * camera1.worldToCameraMatrix;
	}

	void CreateViewMatrix(ref Matrix4x4 viewMatrix,Vector3 look,Vector3 up,Vector3 right,Vector3 pos)
	{
		look.Normalize ();
		up.Normalize ();
		right.Normalize ();
		
		float x = -Vector3.Dot (right,pos);
		float y = -Vector3.Dot (up,pos);
		float z = -Vector3.Dot (look,pos);
		
		viewMatrix.m00 = right.x; viewMatrix.m10 = up.x; viewMatrix.m20 = look.x; viewMatrix.m30 = 0.0f;
		viewMatrix.m01 = right.y; viewMatrix.m11 = up.y; viewMatrix.m21 = look.y; viewMatrix.m31 = 0.0f;
		viewMatrix.m02 = right.z; viewMatrix.m12 = up.z; viewMatrix.m22 = look.z; viewMatrix.m32 = 0.0f;
		viewMatrix.m03 = x;       viewMatrix.m13 = y;    viewMatrix.m23 = z;      viewMatrix.m33 = 1.0f;
	}

	void CreateOrthogonalProjectMatrix(ref Matrix4x4 projectMatrix,Vector3 v3MaxInViewSpace, Vector3 v3MinInViewSpace)
	{
		float scaleX, scaleY, scaleZ;
		float offsetX, offsetY, offsetZ;
		scaleX = 2.0f / (v3MaxInViewSpace.x - v3MinInViewSpace.x);
		scaleY = 2.0f / (v3MaxInViewSpace.y - v3MinInViewSpace.y);
		offsetX = -0.5f * (v3MaxInViewSpace.x + v3MinInViewSpace.x) * scaleX;
		offsetY = -0.5f * (v3MaxInViewSpace.y + v3MinInViewSpace.y) * scaleY;
		scaleZ = 1.0f / (v3MaxInViewSpace.z - v3MinInViewSpace.z);
		offsetZ = -v3MinInViewSpace.z * scaleZ;
		
		//列矩阵
		projectMatrix.m00 = scaleX; projectMatrix.m01 = 0.0f; projectMatrix.m02 = 0.0f; projectMatrix.m03 = offsetX;
		projectMatrix.m10 = 0.0f; projectMatrix.m11 = scaleY; projectMatrix.m12 = 0.0f; projectMatrix.m13 = offsetY;
		projectMatrix.m20 = 0.0f; projectMatrix.m21 = 0.0f; projectMatrix.m22 = scaleZ; projectMatrix.m23 = offsetZ;
		projectMatrix.m30 = 0.0f; projectMatrix.m31 = 0.0f; projectMatrix.m32 = 0.0f; projectMatrix.m33 = 1.0f;
	}
}
